﻿@using System.ComponentModel.DataAnnotations

<MForm @bind-Value="_valid" Model="_model" EnableValidation @ref="_form">
    <MTextField @bind-Value="_model.Name"
                Counter="10"
                Label="Name"
                required></MTextField>
    <MTextField @bind-Value="_model.Email"
                Label="E-mail"
                required></MTextField>
    <MSelect @bind-Value="_model.Item"
             Items="@_items"
             Label="Item"
             ItemText="u => u"
             ItemValue="u => u"></MSelect>
    <MCheckbox @bind-Value="_model.Agree"
               Label="Do you agree?"
               required></MCheckbox>
    <MButton Disabled="!_valid" Color="success" Class="mr-4" OnClick="Validate">Validate</MButton>
    <MButton Color="error" Class="mr-4" OnClick="Reset">Reset Form</MButton>
    <MButton Color="warning" OnClick="ResetValidation">Reset Validation</MButton>
</MForm>

@code {
    class Model
    {
        [Required]
        [MaxLength(10, ErrorMessage = "Name must be less than 10 characters")]
        public string Name { get; set; }
        [Required]
        [EmailAddress]
        public string Email { get; set; }
        [Required]
        public string Item { get; set; }
        [RegularExpression("True", ErrorMessage = "You must agree to continue!")]
        public bool Agree { get; set; }
    }
    private bool _valid = true;
    private MForm _form;
    private Model _model = new();

    List<string> _items = new()
        {
            "Item 1",
            "Item 2",
            "Item 3",
            "Item 4"
        };

    void Validate()
    {
        _form.Validate();
    }

    void Reset()
    {
        _form.Reset();
    }

    void ResetValidation()
    {
        _form.ResetValidation();
    }
}
